clear
capture log close
set more off
set matsize 8000



*** Start with city birthplace data
use data_city_birthplace_data, replace

*** Focus on migration within England, Wales and Scotland
drop if location_of_birth=="born at sea"
drop if location_of_birth=="islands_british"
drop if location_of_birth=="british colonies"
drop if location_of_birth=="foreign"


** Combine Lancashire and Cheshire born population
replace location_of_birth="northwest" if location_of_birth=="lancashire"
replace location_of_birth="northwest" if location_of_birth=="cheshire"

** Combine welsh counties
replace location_of_birth="wales" if location_of_birth=="glamorganshire"
replace location_of_birth="wales" if location_of_birth=="carmarthenshire"
replace location_of_birth="wales" if location_of_birth=="pembrokeshire"
replace location_of_birth="wales" if location_of_birth=="cardiganshire"
replace location_of_birth="wales" if location_of_birth=="brecknockshire"
replace location_of_birth="wales" if location_of_birth=="radnorshire"
replace location_of_birth="wales" if location_of_birth=="montgomeryshire"
replace location_of_birth="wales" if location_of_birth=="flintshire"
replace location_of_birth="wales" if location_of_birth=="denbighshire"
replace location_of_birth="wales" if location_of_birth=="merionetshire"
replace location_of_birth="wales" if location_of_birth=="carnarvonshire"
replace location_of_birth="wales" if location_of_birth=="anglesesy"

count
collapse (sum) pop (mean) total_pop, by(location_of_birth location_of_residence year)
count


*** Merge in county identifiers
rename location_of_residence loc

sort loc
merge loc using data_regions
tab _merge
keep if _merge==3
drop _merge

replace county="northwest" if county=="cheshire"
replace county="northwest" if county=="lancashire"


*** Merge in textile city indicators

sort loc
merge loc using data_town_cotton_shr_1851
tab _merge
keep if _merge==3
drop _merge

*** Drop own-county
count
drop if location_of_birth==county

** Drop counties with cotton towns in them
drop if location_of_birth=="northwest"
drop if location_of_birth=="cumberland"

** Identify cotton textile towns
gen cotton10=0
replace cotton10=1 if shr_cot>.1


*** Drop manchester, which appears to has inconsistent borders in these data
drop if loc=="manchester"

** Collapse and outsheet
collapse (sum) pop (mean) total_pop, by(loc cotton10 year)


collapse (sum) pop total_pop, by(cotton10 year)
keep if cotton10==1
outsheet using results_table_8.csv, comma names replace



***
